<template>
  <el-card class="search">
    <el-form ref="searchForm" :model="searchForm" inline>
      <el-form-item :label="tip + '关键词'" prop="key">
        <el-input v-model.trim="searchForm.key" placeholder="请输入关键词" class="input" clearable />
      </el-form-item>
      <el-form-item :label="select" prop="option">
        <el-select v-model="searchForm.option" placeholder="请选择">
          <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item :label="tip + '状态'" prop="status">
        <el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
          <el-option label="隐藏" :value="0" />
          <el-option label="正常" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button size="medium" @click="searchData">搜索
        </el-button>
        <el-button size="medium" @click="resetSearchData">重置</el-button>
        <el-button type="primary" size="medium" @click="useAdd" v-if="needAdd">新增{{tip}}</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  props: {
    options: Array,
    tip: String,
    select: String,
    needAdd:false
  },
  data() {
    return {
      searchForm: {
        key: '',
        status: '',
        option: '',
      },
      searchContent:{}
    }
  },
  methods: {
    // 新增
    useAdd(){
      this.$emit('add')
    },
    // 重置表单
    resetSearchData() {
      this.$refs['searchForm'].resetFields()
      this.searchContent = {}
    },
    // 搜索
    searchData() {
      Object.keys(this.searchForm).forEach((key) => {
        if (this.searchForm[key] !== '') {
          this.searchContent[key] = this.searchForm[key]
        }
      })

     this.$emit('search', this.searchContent)
    },
  },
}
</script>